﻿using System;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using swWindow = System.Windows.Window;

namespace MimicAPI.DWM
{
    /// <summary>Extensions Methods to use DWM API on Window element</summary>
    public static class DWMExtensions
    {
        public static bool ExtendGlassFrame(this swWindow wnd, Thickness margin)
        {
            // We first check that the AERO theme is enabled
            if (!PInvoke.DwmIsCompositionEnabled())
                return false;

            IntPtr hwnd = new WindowInteropHelper(wnd).Handle;
            if (hwnd == IntPtr.Zero)
                return false;

            wnd.Background = Brushes.Transparent;
            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
            DWM_Rect margins = new DWM_Rect()
            {
                Left = (int)margin.Left,
                Top = (int)margin.Top,
                Right = (int)margin.Right,
                Bottom = (int)margin.Bottom
            };
            hwnd.DwmExtendFrameIntoClientArea(ref margins);
            return true;
        }

        public static bool EnableBlurBehindWindow(this swWindow wnd)
        {
            // We first check that the AERO theme is enabled
            if (!PInvoke.DwmIsCompositionEnabled())
                return false;

            IntPtr hwnd = new WindowInteropHelper(wnd).Handle;
            if (hwnd == IntPtr.Zero)
                return false;

            DWM_Blurbehind objBlur = new DWM_Blurbehind
            {
                fEnable = true,
                fTransitionOnMaximized = true,
                dwFlags = DWM_BlurbehindFlags.Enable | DWM_BlurbehindFlags.TransitionOnMaximized
            };
            hwnd.DwmEnableBlurBehindWindow(ref objBlur);
            return true;
        }
    }
}
